/**
 * @file Define WeChat API methods
 * 
 * @author Steve Gao<gaofu@parllay.com>
 */
const request = require('request');
const async = require('async');
const qs = require('querystring');

const utils = require('../utils');

const WEIBO_ACCESS_TOKEN        = "https://api.weibo.com/oauth2/access_token";
const WEIBO_USER_INFO           = "https://api.weibo.com/2/users/show.json";

class WeiboAPI {

    static getAccessToken(params, parentCallback) {
        let url = `${WEIBO_ACCESS_TOKEN}?${qs.stringify(params)}`

        utils.curlPost(url, {}, (error, data) => {
            if (error) {
                parentCallback(error, null);
            } else {
                parentCallback(null, JSON.parse(data));
            }
        });
    }

    static getUserInfo(accessToken, userId, parentCallback) {
        let url = `${WEIBO_USER_INFO}?access_token=${accessToken}&uid=${userId}`;

        request(url, (error, response, body) => {
            if (error) {
                parentCallback(error, null);
            } else {
                parentCallback(null, JSON.parse(body));
            }
        });
    }
}

exports.WeiboAPI = WeiboAPI;